package com.jsnkj.basic.adapter

import android.app.Activity
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.jsnkj.basic.R
import com.jsnkj.basic.bean.IncomeExpenditureEntity
import com.jsnkj.basic.config.SpConfig


class OutcomeAdapter(
    val context: Activity,
    private val homeItemBeans: List<IncomeExpenditureEntity>
) :
    RecyclerView.Adapter<OutcomeAdapter.HomeViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HomeViewHolder {
        val view: View =
            LayoutInflater.from(parent.context).inflate(R.layout.rv_item_outcome, parent, false)
        return HomeViewHolder(view)
    }

    override fun onBindViewHolder(holder: HomeViewHolder, position: Int) {
        val item = homeItemBeans[position]

        val appInfo: ApplicationInfo

        try {
            appInfo = context.packageManager.getApplicationInfo(
                context.packageName, PackageManager.GET_META_DATA
            )
            holder.tvTitle.text = appInfo.loadLabel(context.packageManager).toString()
        } catch (e: Exception) {
            e.printStackTrace()
        }


        holder.tvRemark.text = item.changed_at
        holder.tvRight.text = "-${item.coins / SpConfig.goldCoinConversion}元"

        holder.tvRightState.text = item.status_lable

        //withdraw_lable
        if (item.withdraw_lable.isNullOrEmpty()) {
            holder.tvInstructions.visibility = View.GONE
        } else {
            holder.tvInstructions.visibility = View.VISIBLE
            holder.tvInstructions.text = "备注：${item.withdraw_lable}"
        }

    }

    override fun getItemCount(): Int {
        return homeItemBeans.size
    }

    class HomeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var tvTitle: TextView
        var tvRemark: TextView
        var tvRight: TextView
        var tvRightState: TextView
        var tvInstructions: TextView

        init {
            tvTitle = itemView.findViewById<TextView>(R.id.tv_title)
            tvRemark = itemView.findViewById<TextView>(R.id.tv_remark)
            tvRight = itemView.findViewById<TextView>(R.id.tv_right)
            tvRightState = itemView.findViewById<TextView>(R.id.tv_right_state)
            tvInstructions = itemView.findViewById<TextView>(R.id.tv_instructions)
        }
    }
}